<!DOCTYPE html>
<html>
<head>
    <title>game</title>
    <style>
        .container { padding: 20px; background: #f5f5f5; }
        .input-group { margin: 10px 0; }
        label { display: inline-block; width: 80px; }
        input, select, button { 
            padding: 8px 12px;
            margin: 5px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        iframe { 
            width: 100%; 
            height: 600px; 
            border: 1px solid #ddd;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 用户名输入 -->
        <div class="input-group">
            <label for="username">username:</label>
            <input type="text" id="username" placeholder="please input username">
        </div>

        <!-- 代理商选择 -->
        <div class="input-group">
            <label for="agency">agency:</label>
            <select id="agency">
                <option value="Y1_INR">Y1_INR</option>
                <option value="Y1_PHP">Y1_PHP</option>
            </select>
        </div>

        <!-- 游戏选择 -->
        <div class="input-group">
            <label for="game">game:</label>
            <select id="game">
                <option value="101">101</option>
                <option value="102">102</option>
                <option value="103">103</option>
                <option value="104">104</option>
                <option value="201">201</option>
                <option value="202">202</option>
                <option value="203">203</option>
                <option value="204">204</option>
                <option value="205">205</option>
                <option value="209">209</option>
            </select>
        </div>

        <button onclick="loadGame()">enter</button>
        <iframe id="gameFrame"></iframe>
    </div>

    <script>
        // 代理商域名配置
        const AGENCY_DOMAINS = {
            Y1_INR: "https://fdsghuk349dfsbjk.ccapi218orbjksapm03fjkds.org/pre/gamemock",
            Y1_PHP: "https://fdsghuk349dfsbjk.ccapi218orbjksapm03fjkds.org/pre/gamemock",
        };

        async function loadGame() {
            // 获取所有输入值
            const username = document.getElementById('username').value;
            const agency = document.getElementById('agency').value;
            const gameId = document.getElementById('game').value;
            
            // 参数校验
            // if (!username.trim()) {
            //     alert('please input username');
            //     return;
            // }

            // 获取域名配置
            const baseUrl = AGENCY_DOMAINS[agency];
            if (!baseUrl) {
                alert('invalid agency');
                return;
            }

            // 构建请求URL（带用户名参数）
            const queryParams = new URLSearchParams({
                agency: agency,
                gameid: gameId,
                _: Date.now()
            });

            // 仅当username非空时添加参数
            if (username) {
                queryParams.append('username', username);
            }

            // 构造目标URL
            const targetUrl = `${baseUrl}/login?${queryParams}`;
            console.info("targetUrl", targetUrl);
            
            try {
                // 发送GET请求
                const response = await fetch(targetUrl, {
                    headers: {
                        'Cache-Control': 'no-cache, no-store, must-revalidate',
                        'Pragma': 'no-cache',
                        'Expires': '0'
                    }
                });
                if (!response.ok) {
                    throw new Error(`请求失败，状态码：${response.status}`);
                }

                // 获取返回的URL（假设返回的是一个URL字符串）
                const resultUrl = await response.text();

                // 设置iframe的src为返回的URL
                gameFrame.src = resultUrl;
                console.info("url", resultUrl);

            } catch (error) {
                console.error('load failed:', error);
                alert(`error:${error.message}`);
            }
        }
    </script>
</body>
</html>